package com.eascs.web.o2o.quartz.util;

import java.util.Date;

import javax.inject.Inject;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StopWatch;
import com.eascs.scheduler.common.entity.ScheduleJob;
import com.eascs.web.o2o.quartz.service.BaseJobService;

public class BaseJobUtil {
	
	
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	
	@Inject
	BaseJobService  baseJobService;
	
	public void jobLogInfo(Date beginDate,String annotionName,String methodName,String isSuccess,String errorMsg){
		ScheduleJob scheduleJob = baseJobService.getJobByName(annotionName, methodName);
		scheduleJob.setIsScucess(isSuccess);
		scheduleJob.setJobError(errorMsg);
		scheduleJob.setStartTime(beginDate);
		scheduleJob.setEndTime(new Date());
		baseJobService.updateEntity(scheduleJob);
	}
	
	/**
	 * 
	 * @Title: executor
	 * @Description: 
	 * @author lwx
	 * @param annotionName类的注解
	 * @param methodName所在的方法名称
	 * @param log日志名称
	 * @param executorService回调接口（用于传入要执行的内容）
	 * @return void
	 * @throws
	 * @date 2017年6月22日 上午11:06:55
	 */
	public void executor(String annotionName,String methodName,String log,ExecutorService executorService){
		StopWatch time = new StopWatch();
		Date beginDate = new Date();
		logger.info(log+"开始任务:[{}]。。。。。。",annotionName+"#"+methodName);
		try {
			//业务逻辑
			executorService.exe();
		} catch (Exception e) {
			jobLogInfo(beginDate,annotionName,methodName,"0",e.getMessage()); 
			logger.info(log+"任务异常:[{}],异常信息：[{}]。。。。。。",annotionName+"#"+methodName,e.getMessage());
			return;
		}
		jobLogInfo(beginDate,annotionName,methodName,"1",null);
		logger.info(log+"结束任务:[{}]。。。。。。,耗时：[{}]",annotionName+"#"+methodName,time.elapsedTime() / 1000 / 60);
	}

}
